package com.smart.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.smart.model.domain.ProjectDO;
import com.smart.model.domain.ProjectReportDO;
import com.smart.model.request.ProjectReportRequest;
import com.smart.model.request.ProjectReportStatisticsRequest;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

/**
 * @author zhanghong
 */
@Repository
public interface ProjectReportMapper extends BaseMapper<ProjectReportDO> {
    ProjectReportDO selectDOById(Long id);
    List<ProjectReportDO> selectDOList(ProjectReportDO projectReport);
    int insertDO(ProjectReportDO projectReport);
    int batchInsertDO(List<ProjectReportDO> projectReportDOList);
    int updateDO(ProjectReportDO projectReport);
    int deleteDOById(Long id);
    int deleteDOByIds(String[] ids);

    @Select({"<script>",
            "   select count(1) from project_report r",
            "   inner join project_temp_task t on r.task_id=t.id",
            "   <where>",
            "       r.deleted=0",
            "       <if test=\"param.projectId!=null\">and r.project_id=#{param.projectId}</if>",
            "       <if test=\"param.projectIds != null and param.projectIds.size() != 0\">",
            "           and r.project_id in",
            "           <foreach collection=\"param.projectIds\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "               #{item} ",
            "           </foreach> ",
            "       </if> ",
            "       <if test=\"param.taskNameOrTaskNum!=null and param.taskNameOrTaskNum!=''\">",
            "           and (t.name like concat('%',#{param.taskNameOrTaskNum},'%') or t.task_num like concat('%',#{param.taskNameOrTaskNum},'%'))",
            "       </if>",
            "       <if test=\"param.status!=null\">and r.status=#{param.status}</if>",
            "       <if test=\"param.itemType!=null and param.itemType!=''\">and r.item_type=#{param.itemType}</if>",
            "   </where>",
            "</script>"})
    long tempPageCount(@Param("param") ProjectReportRequest param);
    @Select({"<script>",
            "   select r.* from project_report r",
            "   inner join project_temp_task t on r.task_id=t.id",
            "   <where>",
            "       r.deleted=0",
            "       <if test=\"param.projectId!=null\">and r.project_id=#{param.projectId}</if>",
            "       <if test=\"param.projectIds != null and param.projectIds.size() != 0\">",
            "           and r.project_id in",
            "           <foreach collection=\"param.projectIds\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "               #{item} ",
            "           </foreach> ",
            "       </if> ",
            "       <if test=\"param.taskNameOrTaskNum!=null and param.taskNameOrTaskNum!=''\">",
            "           and (t.name like concat('%',#{param.taskNameOrTaskNum},'%') or t.task_num like concat('%',#{param.taskNameOrTaskNum},'%'))",
            "       </if>",
            "       <if test=\"param.status!=null\"> and r.status=#{param.status}</if>",
            "       <if test=\"param.itemType!=null and param.itemType!=''\">and r.item_type=#{param.itemType}</if>",
            "   </where>",
            "   order by create_time desc limit #{limit} offset #{offset}",
            "</script>"})
    List<ProjectReportDO> tempPageQuery(@Param("param") ProjectReportRequest param, @Param("limit") int limit, @Param("offset") long offset);

    @Select({"<script>",
            "   select count(1) from project_report r",
            "   inner join project_weekly_task t on r.task_id=t.id",
            "   <where>",
            "       r.deleted=0",
            "       <if test=\"param.projectId!=null\">and r.project_id=#{param.projectId}</if>",
            "       <if test=\"param.projectIds != null and param.projectIds.size() != 0\">",
            "           and r.project_id in",
            "           <foreach collection=\"param.projectIds\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "               #{item} ",
            "           </foreach> ",
            "       </if> ",
            "       <if test=\"param.status!=null\">and r.status=#{param.status}</if>",
            "       <if test=\"param.itemType!=null and param.itemType!=''\">and r.item_type=#{param.itemType}</if>",
            "       <if test=\"param.week!=null and param.week!=0\">and t.week_num=#{param.week}</if>",
            "   </where>",
            "</script>"})
    long weeklyPageCount(@Param("param") ProjectReportRequest param);
    @Select({"<script>",
            "   select r.* from project_report r",
            "   inner join project_weekly_task t on r.task_id=t.id",
            "   <where>",
            "       r.deleted=0",
            "       <if test=\"param.projectId!=null\">and r.project_id=#{param.projectId}</if>",
            "       <if test=\"param.projectIds != null and param.projectIds.size() != 0\">",
            "           and r.project_id in",
            "           <foreach collection=\"param.projectIds\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "               #{item} ",
            "           </foreach> ",
            "       </if> ",
            "       <if test=\"param.status!=null\">and r.status=#{param.status}</if>",
            "       <if test=\"param.itemType!=null and param.itemType!=''\">and r.item_type=#{param.itemType}</if>",
            "       <if test=\"param.week!=null and param.week!=0\">and t.week_num=#{param.week}</if>",
            "   </where>",
            "   order by r.create_time desc limit #{limit} offset #{offset}",
            "</script>"})
    List<ProjectReportDO> weeklyPageQuery(@Param("param") ProjectReportRequest param, @Param("limit") int limit, @Param("offset") long offset);

    /**
     * 统计 周报/临时提报 -已提报数量（已提报的所有都统计，与项目状态无关）
     * @param taskId
     * @return
     */
    Long reportCountRangeProject(@Param("taskId") Long taskId, @Param("projects") Set<Long> projects);
    /**
     * 统计 周报/临时提报 -未提报数量（仅统计项目状态为：在施或者完工待验项目的提报实例数据）
     * @param taskId
     * @return
     */
    Long unReportCountRangeProject(@Param("taskId") Long taskId, @Param("projects") Set<Long> projects);

    /**
     * 查询 周报/临时提报 -任务的所有任务实例数据
     * @param taskId
     * @return
     */
    @Select("select * from project_report where task_id=#{taskId}")
    List<ProjectReportDO> listByTaskId(Long taskId);

    /**
     * 查询 周报/临时提报 -任务的有效任务实例数据【已提报+(未提报且未提报对应的项目状态为在施或者完工待验)】
     * @param param
     * @return
     */
    List<ProjectReportDO> listEffectReportByCondition(@Param("param") ProjectReportStatisticsRequest param);

    /**
     * 查询 周报/临时提报 -项目数据【已提报+(未提报且未提报对应的项目状态为在施或者完工待验)】
     * @param param
     * @return
     */
    List<ProjectDO> listEffectProjectByCondition(@Param("param") ProjectReportStatisticsRequest param);

    @Select("select * from project_report where project_id=#{projectId} and task_id=#{taskId} limit 1")
    ProjectReportDO queryByProjectIdAndTaskId(@Param("projectId") Long projectId, @Param("taskId")Long taskId);

    @Delete("delete from project_report where task_id=#{tempTaskId} and status=0")
    Integer deleteUnReportInstance(Long tempTaskId);

    List<ProjectReportDO> listEffectProjectReportByTaskId(Long tempTaskId);

    @Select("select count(1) from project_report where task_id=#{taskId} and status=1")
    String weeklyCount(Long taskId);

    @Select("select count(1) from project_report where task_id=#{taskId}")
    String mustWeeklyCount(Long taskId);
}
